home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / inventor / inventorTemplates1.1.2 / Highlight.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.2 KB  |  180 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18.  
  19. //
  20. // Source file for "Highlight" node.
  21. //
  22.  
  23. #include <Inventor/SoDB.h>
  24. #include <Inventor/SoDetail.h>
  25. #include <Inventor/events/SoMouseButtonEvent.h>
  26. #include <Inventor/events/SoLocation2Event.h>
  27. #include <Inventor/actions/SoActions.h>
  28. #include "Highlight.h"
  29.  
  30. // Define necessary static type id and field variables
  31. SO_SUBNODE_ID_VARS(Highlight);
  32. SO_SUBNODE_FIELD_VARS(Highlight);
  33.  
  34. // Define required type id, name inquiry, and field methods
  35. SO_SUBNODE_ID_METHODS(Highlight);
  36. SO_SUBNODE_FIELD_METHODS(Highlight);
  37.  
  38. //
  39. // This initializes the Highlight class.
  40. //
  41. void
  42. Highlight::initClass()
  43. {
  44.     SO_SUBNODE_INIT_ID(Highlight, "Highlight", SoSeparator);
  45. }
  46.  
  47. //
  48. // Constructor
  49. //
  50. Highlight::Highlight()
  51. {
  52.     // One-time initialization code:
  53.     SO_SUBNODE_BEGIN_PROTOTYPE(Highlight);
  54.  
  55.     /* If this node had fields, they would be initialized here...    
  56.     // Initialize field data and add fields with default values
  57.     SO_SUBNODE_INIT_FIELDS(Highlight);
  58.     SO_SUBNODE_ADD_FIELD(label, ("<Undefined label>"));
  59.     SO_SUBNODE_ADD_FIELD(minValue, (0.0));
  60.     SO_SUBNODE_ADD_FIELD(maxValue, (1.0));
  61.     SO_SUBNODE_ADD_FIELD(value, (0.0));
  62.     SO_SUBNODE_ADD_FIELD(step, (0.0));
  63.     */
  64.  
  65.     SO_SUBNODE_END_PROTOTYPE();
  66.  
  67.     // The rest of this code is performed for every instance
  68.  
  69.     /*
  70.     // Copy default field values set up in initClass() into this
  71.     // instance
  72.     SO_SUBNODE_INIT_FROM_FIELD_DATA();
  73.     */
  74.  
  75.     highlight = FALSE;
  76. }
  77.  
  78. //
  79. // Destructor
  80. //
  81. Highlight::~Highlight()
  82. {
  83. }
  84.  
  85. //
  86. // Traversal
  87. //
  88. void
  89. Highlight::traverse(SoAction *action)
  90. {
  91.     int nextIndex;
  92.     int code = getTraversalCode(action, nextIndex);
  93.  
  94.     int whichChild = getWhichChild();
  95.  
  96.     if ((whichChild != -1) &&
  97.     ((code != IN_PATH) ||
  98.      (code == IN_PATH && (whichChild <= nextIndex)))) {
  99.         action->pushCurPath(whichChild);
  100.         action->traverse(getChild(whichChild));
  101.         action->popCurPath();
  102.     }
  103. }
  104.  
  105. //
  106. // Returns which child to traverse for the current state of the
  107. // Highlight, -1 if none.
  108. //
  109. int
  110. Highlight::getWhichChild() const
  111. {
  112.     int whichChild = -1;
  113.     int numChildren = getNumChildren();
  114.     
  115.     if (numChildren) {
  116.     
  117.     // If only one child, always draw it
  118.     if (numChildren == 1)
  119.         whichChild = 0;
  120.     
  121.     // If two (or more) children, first is normal, second is highlighted
  122.     else if (numChildren == 2)
  123.         whichChild = (int) highlight;
  124.     }
  125.  
  126.     return whichChild;
  127. }
  128.  
  129.  
  130. //
  131. // Misc actions that will only traverse one of the children.
  132. //
  133. void
  134. Highlight::GLRender(SoGLRenderAction *a)        { traverse(a); }
  135. void
  136. Highlight::rayPick(SoRayPickAction *a)            { traverse(a); }
  137. void
  138. Highlight::getBoundingBox(SoGetBoundingBoxAction *a)    { traverse(a); }
  139. void
  140. Highlight::hiddenLine(SoHiddenLineAction *a)        { traverse(a); }
  141. void
  142. Highlight::painters(SoPaintersAction *a)        { traverse(a); }
  143.  
  144. //
  145. // Handles events on this Highlight nodes
  146. //
  147. void
  148. Highlight::handleEvent(SoHandleEventAction *action)
  149. {
  150.     const SoEvent *event = action->getEvent();
  151.     SbBool isPress = SO_MOUSE_PRESS_EVENT(event, BUTTON1);
  152.     SbBool isMotion =
  153.     event->isOfType(SoLocation2Event::getClassTypeId());
  154.             
  155.     SbBool isHit = FALSE;
  156.     SoDetail *detail = NULL;
  157.     if (isPress || isMotion) {
  158.     detail = action->getDetail();
  159.     if (detail) {
  160.         const SoPath *path = detail->getPath();
  161.         isHit = path->containsNode(this);
  162.     }
  163.     }
  164.  
  165.     SbBool newHighlight = highlight;
  166.  
  167.     if (isPress && isHit) {
  168. //    printf("Ouch! Quit it.\n");
  169.     }
  170.     else if (isMotion)
  171.     newHighlight = isHit;
  172.  
  173.     if (highlight != newHighlight) {
  174.     highlight = newHighlight;
  175.     // Calling touch forces notification, which in turn forces a redraw
  176.     touch();
  177.     }
  178. }
  179.  
  180.